home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: delta / whiteline CD Series - delta.iso / progtool / c / egem_210 / egem / example / preview / preview.c < prev    next >
C/C++ Source or Header  |  1995-11-25  |  7KB  |  344 lines

  1.  
  2. /* Resource-Previewer für EnhancedGEM-Dialoge, (c) 1994 C. Grunenberg
  3.    (läuft als Programm und Accessory) */
  4.  
  5. #include <e_gem.h>
  6. #include <stdio.h>
  7.  
  8. #define CICONBLK ICONBLK
  9.  
  10. #include "prev_rsc.c"
  11. #include "prev_rsc.h"
  12.  
  13. char *entry = "  PreViewer",*name = "PREVIEW";
  14. char *title = "ResourcePreViewer",*small_title = "PreView";
  15. char rsrc_file[MAX_PATH];
  16.  
  17. DIAINFO rsrc_info;
  18. OBJECT *rsrc_tree,**rsrc_trindex;
  19.  
  20. char *rsrc_mem;
  21. int tree,tree_cnt;
  22.  
  23. void Exit(int);
  24. void kill_resource(void);
  25. void load_resource(void);
  26. void Dialog(void);
  27.  
  28. void error(int icon,char *msg)
  29. {
  30.     xalert(1,1,icon,NULL,SYS_MODAL,BUTTONS_RIGHT,TRUE,title,msg,"[Got it!");
  31. }
  32.  
  33. void val_2_str(char *dest,int val)
  34. {
  35.     if (val>=100)
  36.     {
  37.         *dest++ = val/100 + '0';
  38.         val %= 100;
  39.         *dest++ = val/10 + '0';
  40.     }
  41.     else if (val>=10)
  42.         *dest++ = val/10 + '0';
  43.  
  44.     val %= 10;
  45.     *dest++ = val + '0';
  46.     *dest++ = '\0';
  47. }
  48.  
  49. void set_tree_index(void)
  50. {
  51.     tree = min(max(tree,1),tree_cnt);
  52.     val_2_str(ob_get_text(rsrc_tree,TREE,0),tree);
  53.     ob_draw(&rsrc_info,TREE);
  54.  
  55.     val_2_str(ob_get_text(rsrc_tree,TREECNT,0),tree_cnt);
  56.     ob_draw(&rsrc_info,TREECNT);
  57. }
  58.  
  59. void set_dialog(void)
  60. {
  61.     ob_set_text(rsrc_tree,RESOURCE,GetFilename(rsrc_file));
  62.     ob_draw(&rsrc_info,RESOURCE);
  63.     ob_disable(&rsrc_info,rsrc_tree,TESTRSRC,tree_cnt==0,TRUE);
  64.     set_tree_index();
  65. }
  66.  
  67. void Exit(int back)
  68. {
  69.     kill_resource();
  70.  
  71.     if (_app)
  72.         exit_gem(back);
  73.     else
  74.     {
  75.         close_all_windows();
  76.         GetPath(rsrc_file);
  77.         set_dialog();
  78.     }
  79. }
  80.  
  81. int InitMsg(XEVENT *evt,int events_available)
  82. {
  83.     return ((MU_KEYBD|MU_MESAG) & events_available);
  84. }
  85.  
  86. int Messag(XEVENT *event)
  87. {
  88.     reg int ev = event->ev_mwich;
  89.  
  90.     if (ev & MU_MESAG)
  91.     {
  92.         switch (event->ev_mmgpbuf[0])
  93.         {
  94.         case AC_CLOSE:
  95.         case AP_TERM:
  96.             Exit(0);
  97.             break;
  98.         case AC_OPEN:
  99.             Dialog();
  100.             break;
  101.         }
  102.     }
  103.  
  104.     if ((ev & MU_KEYBD) && event->ev_mmokstate==0)
  105.     {
  106.         switch (event->ev_mkreturn & 0xff)
  107.         {
  108.         case '+':
  109.             tree++;
  110.             set_tree_index();
  111.             return (MU_KEYBD);
  112.         case '-':
  113.             tree--;
  114.             set_tree_index();
  115.             return (MU_KEYBD);
  116.         }
  117.     }
  118.  
  119.     return (0);
  120. }
  121.  
  122. char test_title[256];
  123.  
  124. void Dialog(void)
  125. {
  126.     OBJECT *dial,*ob;
  127.     char path[MAX_PATH],fname[MAX_PATH];
  128.     int button,double_click,ex_ob;
  129.  
  130.     if (!open_dialog(rsrc_tree,&rsrc_info,title,small_title,NULL,TRUE,FALSE,WIN_DIAL,0,NULL))
  131.     {
  132.         error(X_ICN_ALERT,"No window available!");
  133.         Exit(-1);
  134.         return;
  135.     }
  136.  
  137.     for (;;)
  138.     {
  139.         button = X_Form_Do(NULL);
  140.  
  141.         switch (button)
  142.         {
  143.         case W_CLOSED:
  144.         case W_ABANDON:
  145.             Exit(0);
  146.             return;
  147.         default:
  148.             double_click = button & 0x8000;
  149.             button &= 0x7fff;
  150.             ob_select(&rsrc_info,rsrc_tree,button,FALSE,TRUE);
  151.  
  152.             switch (button)
  153.             {
  154.             case LOADRSRC:
  155.                 if (FileSelect("Load resource...",path,fname,"*.rsc",TRUE))
  156.                 {
  157.                     MakeFullpath(rsrc_file,path,fname);
  158.                     load_resource();
  159.                     set_dialog();
  160.                 }
  161.                 break;
  162.             case TESTRSRC:
  163.                 dial = rsrc_trindex[tree-1];
  164.                 for (ob=dial,ex_ob=FALSE;;)
  165.                 {
  166.                     if (ob->ob_flags & (EXIT|TOUCHEXIT))
  167.                     {
  168.                         ex_ob = TRUE;
  169.                         break;
  170.                     }
  171.                     else if (ob->ob_flags & LASTOB)
  172.                         break;
  173.                     else
  174.                         ob++;
  175.                 }
  176.  
  177.                 if (ex_ob)
  178.                 {
  179.                     ob_clear_edit(dial);
  180.                     sprintf(test_title,"%s: Tree %d",GetFilename(rsrc_file),tree);
  181.                     xdialog(dial,test_title,NULL,NULL,TRUE,FALSE,ob_isstate(rsrc_tree,WDIAL,SELECTED) ? AUTO_DIAL|MODAL : FLY_DIAL);
  182.                 }
  183.                 else
  184.                     error(X_ICN_ALERT,"No exit-object|or pull-down-menu!");
  185.                 break;
  186.             case EXITRSRC:
  187.                 Exit(0);
  188.                 return;
  189.             case TREEINC:
  190.                 if (double_click)
  191.                     tree = tree_cnt;
  192.                 else
  193.                     tree++;
  194.                 set_tree_index();
  195.                 break;
  196.             case TREEDEC:
  197.                 if (double_click)
  198.                     tree = 0;
  199.                 else
  200.                     tree--;
  201.                 set_tree_index();
  202.             }
  203.         }
  204.     }
  205. }
  206.  
  207. void kill_resource(void)
  208. {
  209.     if (rsrc_mem)
  210.     {
  211.         Mfree(rsrc_mem),
  212.         rsrc_mem = NULL;
  213.         tree = tree_cnt = 0;
  214.     }
  215. }
  216.  
  217. void resource_init(int n_tree,int n_obs,OBJECT *rs_object,OBJECT **rs_trindex)
  218. {
  219.     reg TEDINFO *ted;
  220.     reg ICONBLK *icon;
  221.     reg BITBLK *bit;
  222.     reg OBJECT *obj;
  223.     reg char *mem = rsrc_mem;
  224.     reg int tree,index = n_obs;
  225.  
  226.     for (obj=rs_object,index=n_obs; --index>=0; obj++)
  227.     {
  228.         switch((unsigned char) obj->ob_type)
  229.         {
  230.         case G_TEXT:
  231.         case G_BOXTEXT:
  232.         case G_FTEXT:
  233.         case G_FBOXTEXT:
  234.             ted = obj->ob_spec.tedinfo = (TEDINFO *) (mem + obj->ob_spec.index);
  235.             ted->te_ptext = mem + (long) ted->te_ptext;
  236.             ted->te_ptmplt = mem + (long) ted->te_ptmplt;
  237.             ted->te_pvalid = mem + (long) ted->te_pvalid;
  238.             break;
  239.         case G_BUTTON:
  240.         case G_STRING:
  241.         case G_TITLE:
  242.             obj->ob_spec.free_string = mem + obj->ob_spec.index;
  243.             break;
  244.         case G_IMAGE:
  245.             bit = obj->ob_spec.bitblk = (BITBLK *) (mem + obj->ob_spec.index);
  246.             bit->bi_pdata = (int *) (mem + (long) bit->bi_pdata);
  247.             break;
  248.         case G_ICON:
  249.             icon = obj->ob_spec.iconblk = (ICONBLK *) (mem + obj->ob_spec.index);
  250.             icon->ib_pmask = (int *) (mem + (long) icon->ib_pmask);
  251.             icon->ib_pdata = (int *) (mem + (long) icon->ib_pdata);
  252.             icon->ib_ptext = mem + (long) icon->ib_ptext;
  253.             break;
  254.         }
  255.     }
  256.  
  257.     for (tree=0; tree<n_tree; tree++)
  258.     {
  259.         obj = (OBJECT *) (mem + (long) rs_trindex[tree]);
  260.         rs_trindex[tree] = obj;
  261.  
  262.         for (;;)
  263.         {
  264.             rsrc_obfix(obj,0);
  265.             if (obj->ob_x==0 && obj->ob_width>max_w)
  266.                 obj->ob_width = max_w;
  267.  
  268.             if (obj->ob_flags & LASTOB)
  269.                 break;
  270.             else
  271.                 obj++;
  272.         }
  273.  
  274.         if (ob_isstate(rsrc_tree,EGEMRSRC,SELECTED))
  275.             fix_objects(rs_trindex[tree],TEST_SCALING|DARK_SCALING,FAIL,FAIL);
  276.     }
  277. }
  278.  
  279. void load_resource(void)
  280. {
  281.     reg RSHDR *rsc_hdr;
  282.     reg char *buf,*mem;
  283.     long len,handle;
  284.  
  285.     if ((handle=Fopen(rsrc_file,0))>0)
  286.     {
  287.         len = Fseek(0l,(int) handle,SEEK_END);
  288.         Fseek(0l,(int) handle,SEEK_SET);
  289.  
  290.         if (len<=sizeof(RSHDR))
  291.             error(X_ICN_ERROR,"No resource-file!");
  292.         else if ((mem=Malloc(len+16))!=NULL)
  293.         {
  294.             buf = mem;
  295.             if ((long) buf & 1)
  296.                 buf++;
  297.  
  298.             kill_resource();
  299.             rsc_hdr = (RSHDR *) buf;
  300.  
  301.             if (Fread((int) handle,len,buf)==len && len==rsc_hdr->rsh_rssize)
  302.             {
  303.                 rsrc_mem = mem;
  304.                 rsrc_trindex = (OBJECT **) (rsrc_mem+rsc_hdr->rsh_trindex);
  305.  
  306.                 tree_cnt = rsc_hdr->rsh_ntree;
  307.                 tree = 1;
  308.  
  309.                 resource_init(rsc_hdr->rsh_ntree,rsc_hdr->rsh_nobs,(OBJECT *) (buf+rsc_hdr->rsh_object),rsrc_trindex);
  310.  
  311.                 return;
  312.             }
  313.             else
  314.                 error(X_ICN_DISC_ERR,"Couldn't read resource!");
  315.             Mfree(mem);
  316.         }
  317.         else
  318.             error(X_ICN_ERROR,"Not enough memory!");
  319.         Fclose((int) handle);
  320.     }
  321.     else
  322.         error(X_ICN_DISC_ERR,"Couldn't open resource!");
  323.     GetPath(rsrc_file);
  324. }
  325.  
  326. void main()
  327. {
  328.     if (init_gem(entry,title,name,0,0,0)==TRUE)
  329.     {
  330.         rsrc_init(NUM_TREE,NUM_OBS,NUM_FRSTR,NUM_FRIMG,NO_SCALING,rs_strings,rs_frstr,rs_bitblk,
  331.                   rs_frimg,rs_iconblk,rs_tedinfo,rs_object,(OBJECT **) rs_trindex,(RS_IMDOPE *) rs_imdope,8,16);
  332.         rsrc_tree = (OBJECT *) rs_trindex[PREVIEW];
  333.  
  334.         Event_Handler(InitMsg,Messag);
  335.         CycleCloseWindows('W','U',FAIL,FAIL);
  336.         title_options(FALSE,RED,FALSE);
  337.         set_tree_index();
  338.  
  339.         if (_app)
  340.             Dialog();
  341.         Event_Multi(NULL);
  342.     }
  343. }
  344.